from ctypes import CDLL,byref,create_string_buffer
from ctypes import cdll
from collections import defaultdict
from genericpath import exists
import os, shutil, io, sys, random
import os.path as osp
import json, time
import numpy as np
import cv2
import copy
import base64, requests
from multiprocessing import Pool
from tqdm import tqdm
from PIL import Image
from tool import filesystem, via_tool, darknet_tool, opencv_tool # export PYTHONPATH=$PYTHONPATH:`pwd`

def random_make_data(name, size, cfg_path, dev_code_path):
    dev_codes = []
    with open(dev_code_path, "r") as rf:
        for line in rf.readlines():
            line = line.strip()
            dev_codes.append(line.split("	"))
    
    with open(cfg_path, "r", encoding="utf8") as rf:
        json_data = json.loads(rf.read())

    name_data = json_data.get(name, None)
    if name_data is None:
        print("error, ", name)

    start_time="2023-12-16 08:30:00"
    end_time="2024-12-05 17:30:00"
    start = np.int64(time.mktime(time.strptime(start_time, '%Y-%m-%d %H:%M:%S')))
    end = np.int64(time.mktime(time.strptime(end_time, '%Y-%m-%d %H:%M:%S')))
    step = (end - start) / size

    datas = []
    for idx in range(size):

        happen_time = start + step * idx + random.randint(-60 * 60 * 48, 60 * 60 * 48)
        # 加时区
        happen_time += 60*60*8
        happen_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(happen_time))
        hour = (happen_time % (60*60*24)) / (60*60)
        diff = 0
        if hour < 8:
            diff = 60*60 * 8
        if hour > 18:
            diff = -60*60 * 8
        happen_time += diff
        happen_time_str2 = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(happen_time))

        dev_code, location_name = random.choice(dev_codes)
        alarm_type_json = random.choice(name_data["algDescriptiveInfos"])
        alarm_type = alarm_type_json["type"]
        alarm_level = alarm_type_json["level"]
        alarm_note = alarm_type_json["note"]
        datas.append([happen_time_str2, dev_code, location_name, name, alarm_type, alarm_level, alarm_note])

    datas = sorted(datas, key=lambda x: x[0])

    with open(name+".xls", "w") as wf:
        for data in datas:
            wf.write("\t".join(data) + "\n")


if __name__ == "__main__":

    name = "操作带电设备时未戴绝缘手套"
    size = 7
    cfg_path = r"D:\work\code\python_script\sunjie\qinghai\algo.json"
    dev_code_path = r"D:\work\code\python_script\sunjie\qinghai\devcode.txt"
    random_make_data(name, size, cfg_path, dev_code_path)
